package com.sz.common.base.dao.ext.impl;

import com.sz.common.base.dao.Dao;
import com.sz.common.base.dao.ext.VersionEntityMapperDao;
import com.sz.common.base.entity.VersionEntity;
import com.sz.common.base.utils.DateUtils;
import com.sz.common.base.utils.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Package: com.sz.common.base.dao.ext.impl <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-03-16 10:31:00.
 */
@Service
public class VersionEntityMapperDaoSupport implements VersionEntityMapperDao {

    @Autowired
    protected Dao dao;

    /**
     * @param insertSql
     * @param entity
     */
    @Override
    public void insertEntity(String insertSql, VersionEntity entity) {
        if (entity == null) {
            throw new IllegalArgumentException("entity cannot be empty");
        }
        List<VersionEntity> entities = new ArrayList<>();
        entities.add(entity);
        insertEntities(insertSql, entities);
    }

    /**
     * @param updateSql
     * @param insertSql
     * @param entity
     */
    @Override
    public void updateEntity(String updateSql, String insertSql, VersionEntity entity) {
        if (entity == null) {
            throw new IllegalArgumentException("entity cannot be empty");
        }
        List<VersionEntity> entities = new ArrayList<>();
        entities.add(entity);
        updateEntities(updateSql, insertSql, entities);
    }

    /**
     * @param updateSql
     * @param softDeleteSQL
     * @param uid
     */
    @Override
    public void deleteEntity(String updateSql, String softDeleteSQL, String uid) {
        List<String> uids = new ArrayList<>();
        uids.add(uid);
        deleteEntities(updateSql, softDeleteSQL, uids);
    }

    private void inactiveVersion(String updateSql, List<String> uids, Date date) {
        if (!StringUtils.hasLength(updateSql)
                || uids == null || uids.size() == 0) {
            throw new IllegalArgumentException("sql and uid cannot be empty");
        }
        if (date == null) {
            date = dao.getDbDate();
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (String uid : uids) {
            Map<String, Object> map = new HashMap<>();
            map.put("isActive", false);
            map.put("inactiveTime", date);
            map.put("uid", uid);
            list.add(map);
        }
        this.dao.batchUpdate(updateSql, list);
    }

    /**
     * @param insertSql
     * @param entities
     */
    @Override
    public void insertEntities(String insertSql, List<VersionEntity> entities) {
        if (!StringUtils.hasLength(insertSql)) {
            throw new IllegalArgumentException("sql and entity cannot be empty");
        }
        if (entities == null || entities.size() == 0) {
            throw new IllegalArgumentException("entities cannot be empty");
        }
        Date date = dao.getDbDate();
        for (VersionEntity entity : entities) {
            if (!StringUtils.hasLength(entity.getUid())) {
                entity.setUid(UUIDGenerator.generateOrderedUUID());
            }
            entity.setIsActive(true);
            if (entity.getActiveTime() == null) {
                entity.setActiveTime(date);
            }
            if (entity.getInactiveTime() == null) {
                entity.setInactiveTime(DateUtils.getFinalDate());
            }
            if (entity.getLastUpdateTime() == null) {
                entity.setLastUpdateTime(entity.getActiveTime());
            }
            if (entity.getCreateTime() == null) {
                entity.setCreateTime(entity.getActiveTime());
            }
        }
        this.dao.batchInsert(insertSql, entities);
    }

    /**
     * @param updateSql
     * @param insertSql
     * @param entities
     */
    @Override
    public void updateEntities(String updateSql, String insertSql, List<VersionEntity> entities) {
        if (!StringUtils.hasLength(updateSql) || !StringUtils.hasLength(insertSql)) {
            throw new IllegalArgumentException("sql cannot be empty");
        }
        if (entities == null || entities.size() == 0) {
            throw new IllegalArgumentException("entities cannot be empty");
        }

        Date date = dao.getDbDate();
        List<String> uids = new ArrayList<>();
        for (VersionEntity entity : entities) {
            uids.add(entity.getUid());
            entity.setId(null);
            entity.setLastUpdateTime(null);
            entity.setIsActive(true);
            entity.setActiveTime(date);
            entity.setInactiveTime(DateUtils.getFinalDate());
        }
        inactiveVersion(updateSql, uids, date);
        // insert new entity
        this.dao.batchInsert(insertSql, entities);
    }

    /**
     * @param updateSql
     * @param softDeleteSql
     * @param uids
     */
    @Override
    public void deleteEntities(String updateSql, String softDeleteSql, List<String> uids) {
        if (!StringUtils.hasLength(updateSql) || !StringUtils.hasLength(softDeleteSql)) {
            throw new IllegalArgumentException("sql and uid cannot be empty");
        }
        if (uids == null || uids.size() == 0) {
            throw new IllegalArgumentException("uid cannot be empty");
        }
        inactiveVersion(updateSql, uids, null);
        this.dao.batchUpdate(softDeleteSql, uids);
    }
}
